// ==========================================================================
// 云捷GO自动生成model扩展代码列表、增、删，改、查、导出，只生成一次，按需修改,再次生成不会覆盖.
// 生成日期：2021-07-09 11:02:15
// 生成路径: app/model/module/category/category.go
// 生成人：gfcms
// ==========================================================================
package category

import (
	"github.com/gogf/gf/database/gdb"
	"github.com/gogf/gf/errors/gerror"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/os/gcache"
	"myblog/app/utils/page"
	"time"
)

// Entity is the golang structure for table sys_menu.
type EntityExtend struct {
	Entity
	ParentName string         `json:"parentName"` // 父菜单名称
	Children   []EntityExtend `json:"children"`   // 子菜单
}

//新增页面请求参数
type AddReq struct {
	Parentid     int64  `p:"parentid" `
	Catname      string `p:"catname" v:"required#栏目名称不能为空"`
	Image        string `p:"image" `
	Description  string `p:"description" `
	ListTemplate string `p:"list_template"`
	ShowTemplate string `p:"show_template"`
	Listorder    int    `p:"listorder" `
	Ismenu       int    `p:"ismenu" `
}

//修改页面请求参数
type EditReq struct {
	Id           int64  `p:"id" v:"required#主键ID不能为空"`
	Parentid     int64  `p:"parentid" `
	Catname      string `p:"catname" v:"required#栏目名称不能为空"`
	Image        string `p:"image" `
	Oldimage     string `p:"oldimage"`
	Description  string `p:"description" `
	ListTemplate string `p:"list_template"`
	ShowTemplate string `p:"show_template"`
	Listorder    int    `p:"listorder" `
	Ismenu       int    `p:"ismenu" `
}

//分页请求参数 
type SelectPageReq struct {
	Parentid      int64  `p:"parentid"`      //父级id
	Catname       string `p:"catname"`       //栏目名称
	Image         string `p:"image"`         //栏目图片
	Description   string `p:"description"`   //描述
	Listorder     int    `p:"listorder"`     //排序
	Ismenu        int    `p:"ismenu"`        //是否导航栏显示
	BeginTime     string `p:"beginTime"`     //开始时间
	EndTime       string `p:"endTime"`       //结束时间
	PageNum       int    `p:"pageNum"`       //当前页码
	PageSize      int    `p:"pageSize"`      //每页数
	OrderByColumn string `p:"orderByColumn"` //排序字段
	IsAsc         string `p:"isAsc"`         //排序方式
}

//根据条件分页查询数据
func SelectListByPage(param *SelectPageReq) ([]Entity, *page.Paging, error) {
	db, err := gdb.Instance()

	if err != nil {
		return nil, nil, gerror.New("获取数据库连接失败")
	}

	model := db.Table("t_category t")

	if param != nil {

		if param.Catname != "" {
			model.Where("t.catname like ?", "%"+param.Catname+"%")
		}

		if param.Image != "" {
			model.Where("t.image = ?", param.Image)
		}

		if param.Description != "" {
			model.Where("t.description = ?", param.Description)
		}

		if param.BeginTime != "" {
			model.Where("date_format(t.create_time,'%y%m%d') >= date_format(?,'%y%m%d') ", param.BeginTime)
		}

		if param.EndTime != "" {
			model.Where("date_format(t.create_time,'%y%m%d') <= date_format(?,'%y%m%d') ", param.EndTime)
		}
	}

	total, err := model.Count()

	if err != nil {
		return nil, nil, gerror.New("读取行数失败")
	}

	page := page.CreatePaging(param.PageNum, param.PageSize, total)

	model.Limit(page.StartNum, page.Pagesize)

	if param.OrderByColumn != "" {
		model.Order(param.OrderByColumn + " " + param.IsAsc)
	}

	var result []Entity
	model.Structs(&result)
	return result, page, nil
}

// 导出excel
func SelectListExport(param *SelectPageReq) (gdb.Result, error) {
	db, err := gdb.Instance()

	if err != nil {
		return nil, gerror.New("获取数据库连接失败")
	}

	model := db.Table("t_category t")

	if param != nil {

		if param.Catname != "" {
			model.Where("t.catname like ?", "%"+param.Catname+"%")
		}

		if param.Image != "" {
			model.Where("t.image = ?", param.Image)
		}

		if param.Description != "" {
			model.Where("t.description = ?", param.Description)
		}

		if param.BeginTime != "" {
			model.Where("date_format(t.create_time,'%y%m%d') >= date_format(?,'%y%m%d') ", param.BeginTime)
		}

		if param.EndTime != "" {
			model.Where("date_format(t.create_time,'%y%m%d') <= date_format(?,'%y%m%d') ", param.EndTime)
		}
	}

	//"","模型id","父级id","栏目名称","栏目图片","描述","栏目url","","设置","排序","是否导航栏显示","创建时间","更新时间",
	model.Fields(" t.id ,(SELECT catname FROM t_category WHERE id = t.parentid) parent_name ,t.catname ,t.image ,t.description ,t.listorder ,t.ismenu ,t.create_time ,t.update_time")

	result, _ := model.All()
	return result, nil
}

//获取所有数据
func SelectListAll(param *SelectPageReq) ([]Entity, error) {
	db, err := gdb.Instance()

	if err != nil {
		return nil, gerror.New("获取数据库连接失败")
	}

	model := db.Model("t_category t")

	if param != nil {

		if param.Catname != "" {
			model.Where("t.catname like ?", "%"+param.Catname+"%")
		}

		if param.Image != "" {
			model.Where("t.image = ?", param.Image)
		}

		if param.Description != "" {
			model.Where("t.description = ?", param.Description)
		}

		if param.BeginTime != "" {
			model.Where("date_format(t.create_time,'%y%m%d') >= date_format(?,'%y%m%d') ", param.BeginTime)
		}

		if param.EndTime != "" {
			model.Where("date_format(t.create_time,'%y%m%d') <= date_format(?,'%y%m%d') ", param.EndTime)
		}
		model.Order("parentid")
	}

	var result []Entity
	model.Structs(&result)
	return result, nil
}

func Catelist(pid int) ([]CateList, error) {
	var catelist []CateList
	res, err := gcache.Get("catelist")
	if err != nil {
		return nil, err
	}
	if res == nil {
		model := g.Model("t_category")
		model.Fields("id,parentid,catname,ismenu").Where("ismenu=?", 1).Where("parentid", pid).FindAll()
		err := model.Structs(&catelist)
		if err != nil {
			return nil, err
		}
		gcache.Set("catelist", catelist, 30*time.Minute)
	} else {
		catelist = res.([]CateList)
	}

	return catelist, nil
}
